﻿using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Exceptions;
using gdlmo.DNN.Modules.Informations.Business;

namespace gdlmo.DNN.Modules.Informations
{
	public abstract class Informations : PortalModuleBase, IActionable
	{		
		protected System.Web.UI.WebControls.LinkButton cmdFirst;
		protected System.Web.UI.WebControls.LinkButton cmdPrev;
		protected System.Web.UI.WebControls.DropDownList cboPages;
		protected System.Web.UI.WebControls.LinkButton cmdNext;
		protected System.Web.UI.WebControls.LinkButton cmdLast;
		protected System.Web.UI.HtmlControls.HtmlTable tbMore;
		protected System.Web.UI.WebControls.Repeater rptInfos;
		protected System.Web.UI.WebControls.HyperLink lnkMore;
		protected System.Web.UI.HtmlControls.HtmlTable tbPage;

		#region Private Member
		
		private int MaxChars
		{
			get 
			{
				object o = ViewState["MaxChars"];
				if (o == null) 
				{
					return 20;
				}
				return (int)o;
			}
			set 
			{				
				ViewState["MaxChars"] = value;
			}
		}	
		private int PageSize
		{
			get 
			{
				object o = ViewState["PageSize"];
				if (o == null) 
				{
					return 10;
				}
				return (int)o;
			}
			set 
			{				
				ViewState["PageSize"] = value;
			}
		}	
		/// <summary>
		/// 页索引，从0开始
		/// </summary>
		private int PageIndex
		{
			get 
			{
				object o = ViewState["PageIndex"];
				if (o == null) 
				{
					return 0;
				}
				return (int)o;
			}

			set 
			{				
				ViewState["PageIndex"] = value;
			}
		}
		private int PageCount
		{
			get
			{
				object o = ViewState["PageCount"];
				if (o == null) 
				{
					return 0;
				}
				return (int)o;
			}
			set
			{
				ViewState["PageCount"] = value;
			}
		}
		#endregion

		#region Private Function
		protected int GetMaxChars()
		{
			return this.MaxChars;
		}		
		
		/// <summary>
		/// 是否显示分页
		/// </summary>
		/// <param name="PageShow"></param>
//		private void IsPageShow(Boolean PageShow)
//		{
//			tbPage.Visible = PageShow;
//			tbMore.Visible = !tbPage.Visible;
//		}
		/// <summary>
		/// 处理按钮
		/// </summary>
		private void ProessButton()
		{
			if (this.PageIndex == 0)
			{
				this.cmdFirst.Enabled = false;
				this.cmdPrev.Enabled = false;
			}
			else
			{
				this.cmdFirst.Enabled = true;
				this.cmdPrev.Enabled = true;
			}
			if (this.PageIndex == (this.PageCount - 1))
			{
				this.cmdNext.Enabled = false;
				this.cmdLast.Enabled = false;
			}
			else
			{
				this.cmdNext.Enabled = true;
				this.cmdLast.Enabled = true;
			}
			if (this.PageCount == 0)
			{
				this.cmdFirst.Enabled = false;
				this.cmdPrev.Enabled = false;
				this.cmdNext.Enabled = false;
				this.cmdLast.Enabled = false;
			}
		}
		protected void BindList()
		{
			InformationsController objCtlInfo = new InformationsController();			
			int pcount=0;			
			rptInfos.DataSource = objCtlInfo.GetPagingData(this.PageIndex,this.PageSize,ref pcount);		
			this.PageCount = pcount;
			if(cboPages.Items.Count!=this.PageCount)
			{
				cboPages.Items.Clear();
				for(int i=0;i<this.PageCount;i++)
				{
					cboPages.Items.Add("第" +(i+1).ToString()+"页");
				}
			}	
			rptInfos.DataBind();
			ProessButton();
			cboPages.SelectedIndex = this.PageIndex;
		}

		private void PageBtnClicked(object sender, EventArgs e)					
		{
			string BtnName = ((LinkButton) sender).CommandArgument;			
			if (string.Compare(BtnName, "First", false) == 0)
			{
				this.PageIndex = 0;
			}
			else if (string.Compare(BtnName, "Prev", false) == 0)
			{
				if (this.PageIndex > 0)
				{
					this.PageIndex--;
				}
			}
			else if (string.Compare(BtnName, "Next", false) == 0)
			{
				if (this.PageIndex < (this.PageCount - 1))
				{
					this.PageIndex++;
				}
			}
			else if (string.Compare(BtnName, "Last", false) == 0)
			{
				this.PageIndex = this.PageCount - 1;
			}			
			this.BindList();
		}
		protected bool MyIsEditable(string CreatedByUser)
		{
			if(UserInfo.UserID != -1)
			{
				if(UserInfo.UserID.ToString().CompareTo(CreatedByUser.ToLower())==0 || DotNetNuke.Security.PortalSecurity.IsInRole("Administrators") )
				{
					return true;
				}
			}
			return false;
		}
		#endregion

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.cmdFirst.Click += new System.EventHandler(this.PageBtnClicked);
			this.cmdPrev.Click += new System.EventHandler(this.PageBtnClicked);
			this.cboPages.SelectedIndexChanged += new System.EventHandler(this.cboPages_SelectedIndexChanged);
			this.cmdNext.Click += new System.EventHandler(this.PageBtnClicked);
			this.cmdLast.Click += new System.EventHandler(this.PageBtnClicked);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		#region Event Handlers

		private void Page_Load(object sender, System.EventArgs e)
		{
			try 
			{
				
				if (!Page.IsPostBack) 
				{
	
					if (Settings["MaxChars"]!=null)					//每行最大字符数
					{
						if(gdlmo.Common.StringHelper.IsNum(Settings["MaxChars"].ToString()))	MaxChars = Int32.Parse(Settings["MaxChars"].ToString());
						else MaxChars = 0;
					}	
					if (Settings["PageSize"]!=null)				
					{
						if(gdlmo.Common.StringHelper.IsNum(Settings["PageSize"].ToString()))		PageSize = Int32.Parse(Settings["PageSize"].ToString());
						else PageSize = 0;
					}		
					BindList();					
				}
			} 
			catch (Exception exc) 
			{
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}

		private void cboPages_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			this.PageIndex = cboPages.SelectedIndex;
			this.BindList();
		}
		#endregion

		#region Optional Interfaces

		public DotNetNuke.Entities.Modules.Actions.ModuleActionCollection ModuleActions 
		{
			get 
			{
				DotNetNuke.Entities.Modules.Actions.ModuleActionCollection Actions = new DotNetNuke.Entities.Modules.Actions.ModuleActionCollection();
				Actions.Add(GetNextActionID(), Localization.GetString(DotNetNuke.Entities.Modules.Actions.ModuleActionType.AddContent, LocalResourceFile), DotNetNuke.Entities.Modules.Actions.ModuleActionType.AddContent, "", "", EditUrl(), false, DotNetNuke.Security.SecurityAccessLevel.Edit, true, false);
				return Actions;
			}
		}
		#endregion		
	}
}